package com.bdqn.service;

import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.Page;
import com.bdqn.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService{

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 根据账号密码登录查询
     * @param account 账号
     * @param password 密码
     * @return
     */
    @Override
    public SysUser getSysUserByAccount(String account, String password) {
        if(account == null || password == null){
            return null;
        }
        return sysUserMapper.getByAccountAndPassword(account,password);
    }

    @Override
    public int getPageListTotal(SysUser sysUser) {
        return sysUserMapper.getPageListTotal(sysUser);
    }

    /**
     * 分页显示数据
     * @param sysUser
     * @param page
     * @return
     */
    @Override
    public List<SysUser> getPageList(SysUser sysUser, Page page) {
        int pageIndex=(page.getCurrPageNo()-1) * page.getPageSize();
        return sysUserMapper.getPageList(sysUser,pageIndex,page.getPageSize());
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @Override
    public int getDelete(int id) {
        return sysUserMapper.getDelete(id);
    }

    @Override
    public SysUser getAccount(String account) {
        return sysUserMapper.getAccount(account);
    }

    //保存用户
    @Override
    public boolean add(SysUser sysUser) {
        sysUser.setCreatedTime(new Date());  //设置创建时间
        int result = sysUserMapper.getAdd(sysUser);
        return (result==1);
    }

    /**
     * 根据用户id查询（用户详情）
     * @param id
     * @return
     */
    @Override
    public SysUser getId(int id) {
        return sysUserMapper.getId(id);
    }

    /**
     * 修改用户
     * @param sysUser
     * @return
     */
    @Override
    public Boolean update(SysUser sysUser) {
        sysUser.setUpdatedTime(new Date()); // 设置修改时间
        return sysUserMapper.update(sysUser);
    }

}
